/*
 * Copyright (c) 2016, Inspur, All Rights Reserved.
 */

package com.pazu.hibernate.demo.core.dao;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.pazu.hibernate.demo.core.pojo.BasePojo;

/**
 * <br/>
 * Date: 2016年2月4日 下午3:03:12 <br/>
 * @author Administrator
 * @since v5.0
 * @see
 */
@Resource
public class BaseDaoImpl<T extends BasePojo> implements BaseDao<T> {
    @Autowired
    private HibernateTemplate hibernateTemplate;
    
    public T create(T t) {
        hibernateTemplate.save(t);
        return t;
    }

    public T update(T t) {
        hibernateTemplate.update(t);
        return t;
    }

    public void delete(T t) {
        hibernateTemplate.delete(t);
    }

    public T getById(Class<T> clazz, String id) {
        return (T)hibernateTemplate.get(clazz, id);
    }

    public void deleteById(String id) {
        if (StringUtils.isBlank(id))
            throw new RuntimeException("根据id[" + id + "]删除pojo出错：id不能为空！");
    }
    
    public HibernateTemplate getTemplate() {
        return hibernateTemplate;
    }
}
